//
//  BNAccountComponent.m
//  Banknote
//
//  Created by tanxiaokang on 2017/11/14.
//  Copyright © 2017年 runnongjinfu. All rights reserved.
//

#import "BNAccountComponent.h"

static NSString *const BNUserAccountKey = @"BNUserAccountKey";      //用户账号中心
static NSString *const BNConfigCenterKey = @"BNConfigCenterkey";    //配置中心

static NSString *const BNUser_IdKey = @"USER_ID";
static NSString *const BNUser_LoginKey = @"User_Login";
static NSString *const BNUser_PhoneKey = @"User_Phone";
static NSString *const BNUser_TokenKey = @"token";
static NSString *const BNUser_IDCardKey = @"IDCard";
static NSString *const BNUser_TrueNameKey = @"TrueName";
static NSString *const BNUser_NotReadMessageKey = @"NotReadMessage";

#define DEFAULTSGET(key) [self.userAccount objectForKey:key]
#define DEFAULTSSET(obj,key) [self.userAccount mdf_safeSetObject:obj forKey:key]

static NSString *const BNConfigCenter_updataKey = @"BNConfigUpdataKey";
static NSString *const BNConfigCenter_lastLoginPhoneNumberKey = @"BNConfiglastLoginPhoneNumberKey";
#define DEFAULTSGET_config(key) [self.configConter objectForKey:key]
#define DEFAULTSSET_config(obj,key) [self.configConter mdf_safeSetObject:obj forKey:key]

@interface BNAccountComponent ()

@property(nonatomic,strong) NSMutableDictionary *userAccount;
@property(nonatomic,strong) NSMutableDictionary *configConter;

@end

@implementation BNAccountComponent

- (void)save:(NSDictionary *)dict Key:(NSString *)key{
    
    NSLog(@"保存：%@",dict);
    [[NSUserDefaults standardUserDefaults] setObject:dict forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"保存后：%@",[[NSUserDefaults standardUserDefaults] objectForKey:key]);
}

- (void)clear:(NSDictionary *)dict Key:(NSString *)key{
    
    NSLog(@"清除前：%@",[[NSUserDefaults standardUserDefaults] objectForKey:key]);
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"清除后：%@",[[NSUserDefaults standardUserDefaults] objectForKey:key]);
    self.userAccount = nil;
}

#pragma mark - UserAccountCenter

#pragma mark get
- (NSString *)user_ID {

    return DEFAULTSGET(BNUser_IdKey);
}

- (NSString *)user_Phone{

    return DEFAULTSGET(BNUser_PhoneKey);
}

- (NSString *)user_Token{

    return DEFAULTSGET(BNUser_TokenKey);
}

- (NSString *)user_IDCard{

    return DEFAULTSGET(BNUser_IDCardKey);
}

- (NSString *)user_TrueName {

    return DEFAULTSGET(BNUser_TrueNameKey);
}

- (BOOL)isLogin {
    
    BOOL loginState = (BOOL)DEFAULTSGET(BNUser_LoginKey);
    return loginState;
}

- (BOOL)user_NotReadMessage {
    
    NSNumber *message = DEFAULTSGET(BNUser_NotReadMessageKey);
    if (message.stringValue.integerValue == 0) {
        return NO;
    } else {
        return YES;
    }
}

#pragma mark set
-(void)setUser_ID:(NSString *)user_ID {

    DEFAULTSSET(user_ID, BNUser_IdKey);
}

- (void)setUser_Phone:(NSString *)user_Phone {

    DEFAULTSSET(user_Phone, BNUser_PhoneKey);
}

- (void)setUser_Token:(NSString *)user_Token{

    DEFAULTSSET(user_Token, BNUser_TokenKey);
}

- (void)setUser_IDCard:(NSString *)user_IDCard {

    DEFAULTSSET(user_IDCard, BNUser_IDCardKey);
}

- (void)setUser_TrueName:(NSString *)user_TrueName {

    DEFAULTSSET(user_TrueName, BNUser_TrueNameKey);
}

- (void)setIsLogin:(BOOL)isLogin {

    if (isLogin) {
        DEFAULTSSET(@(1), BNUser_LoginKey);
    } else {
        DEFAULTSSET(@(0), BNUser_LoginKey);
    }
}

- (void)setUser_NotReadMessage:(BOOL)user_NotReadMessage {
    NSLog(@"写入未读消息：%d",user_NotReadMessage);
    if (user_NotReadMessage) {
        DEFAULTSSET(@(1), BNUser_NotReadMessageKey);
    } else {
        DEFAULTSSET(@(0), BNUser_NotReadMessageKey);
    }
}

- (NSMutableDictionary *)userAccount {
    
    if (!_userAccount) {
    
        _userAccount = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults]objectForKey:BNUserAccountKey]];
        
        if (!_userAccount) {
            _userAccount = [[NSMutableDictionary alloc] init];
        }
    }
    return _userAccount;
}

- (void)save_user{
    [self save:self.userAccount Key:BNUserAccountKey];
}

- (void)clear_user{
    [self clear:self.userAccount Key:BNUserAccountKey];
}

#pragma mark - BNConfigCenter

- (NSInteger)configUpdataType {

    return [DEFAULTSGET_config(BNConfigCenter_updataKey) integerValue];
}

- (void)setConfigUpdataType:(NSInteger)configUpdataType {

    DEFAULTSSET_config(@(configUpdataType), BNConfigCenter_updataKey);
}

- (NSString *)config_lastLoginPhoneNumber {

    return DEFAULTSGET_config(BNConfigCenter_lastLoginPhoneNumberKey);
}

- (void)setConfig_lastLoginPhoneNumber:(NSString *)config_lastLoginPhoneNumber {

    DEFAULTSSET_config(config_lastLoginPhoneNumber, BNConfigCenter_lastLoginPhoneNumberKey);
}

- (NSMutableDictionary *)configConter {
    
    if (!_configConter) {
        
        _configConter = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults]objectForKey:BNConfigCenterKey]];
        
        if (!_configConter) {
            _configConter = [[NSMutableDictionary alloc] init];
        }
    }
    return _configConter;
}

- (void)save_config {
    [self save:self.configConter Key:BNConfigCenterKey];
}

- (void)clear_config {
    [self clear:self.configConter Key:BNConfigCenterKey];
}

#pragma mark - other

- (void)setUserInfo:(NSDictionary *)userData{
    
    [BNAccount setIsLogin:YES];
    [BNAccount setUser_ID:userData[@"userNo"]];
    [BNAccount setUser_Phone:userData[@"mobile"]];
    [BNAccount setUser_Token:userData[@"token"]];
    [BNAccount setConfig_lastLoginPhoneNumber:userData[@"mobile"]];
    [BNAccount setUser_NotReadMessage:YES];
    
    [BNAccount save_user];
}

@end
